package com.masblearn;

import java.awt.*;
import java.util.Random;

public class Tank extends GameObject {
//    public int x, y;
    private int oldX,oldY;
    public Dir dir = Dir.UP;
    public static final int SPEED = 3;
    //是否在移动
    public boolean moving = true;
    //tankframe
//    public TankFrame tf = null;
    private boolean living = true;
    //坦克高度和宽度
    public static int TANK_WIDTH = ResourceManager.goodTankU.getWidth();
    public static int TANK_HEIGHT = ResourceManager.goodTankU.getHeight();

    private Random random = new Random();
    public Group group = Group.BAD;

    public Rectangle rectangle = new Rectangle();

    public Tank(int x, int y, Dir dir, Group group ) {
        this.x = x;
        this.y = y;
        this.dir = dir;
//        this.tf = tf;

        this.group = group;

        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = TANK_WIDTH;
        rectangle.height = TANK_HEIGHT;
        GameModel.getInstance().add(this);
    }

    @Override
    public void paint(Graphics g) {
        if (!living) GameModel.getInstance().remove(this);
        //根据方向来画
        switch (dir) {
            case UP:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankU : ResourceManager.badTankU, x, y, null);
                break;
            case DOWN:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankD : ResourceManager.badTankD, x, y, null);
                break;
            case LEFT:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankL : ResourceManager.badTankL, x, y, null);
                break;
            case RIGHT:
                g.drawImage(this.group == Group.GOOD ? ResourceManager.goodTankR : ResourceManager.badTankR, x, y, null);
                break;
            default:
                break;
        }

        move();
    }

    @Override
    public int getWidth() {
        return TANK_WIDTH;
    }

    @Override
    public int getHeight() {
        return TANK_HEIGHT;
    }

    public void back(){
        x = oldX;
        y = oldY;
    }

    private void move() {
        //记录移动前的位置
        oldX = x;
        oldY = y;

        if (!moving) return;
        if (dir == Dir.UP) {
            y -= SPEED;

        }
        if (dir == Dir.DOWN) {
            y += SPEED;
        }
        if (dir == Dir.RIGHT) {
            x += SPEED;
        }
        if (dir == Dir.LEFT) {
            x -= SPEED;
        }
        if (this.group == Group.BAD && random.nextInt(100) > 95) {
            this.fire(DefaultFireStrategy.getInstance());
        }
        //随机方向
        if (this.group == Group.BAD && random.nextInt(100) > 95) {
            randomDir();
        }
//        边界检查
        boundsCheck();
        //更新
        rectangle.x = this.x;
        rectangle.y = this.y;
        rectangle.width = TANK_WIDTH;
        rectangle.height = TANK_HEIGHT;
    }

    private void boundsCheck() {
        if (this.x < 0) x = 0;
        else if (this.y < 30) y = 30;
        else if (this.x > TankFrame.GAME_WIDTH - Tank.TANK_WIDTH) x = TankFrame.GAME_WIDTH - Tank.TANK_WIDTH;
        else if (this.y > TankFrame.GAME_HEIGHT - Tank.TANK_HEIGHT) y = TankFrame.GAME_HEIGHT - Tank.TANK_HEIGHT;
    }

    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }


    //开火
    //策略模式.可以使用方法传参也可以，设为变量
    public void fire(FireStrategy fireStrategy) {
        fireStrategy.fire(this);
    }

    public void die() {
        this.living = false;
    }

    public void stop() {
        moving = false;
    }


    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }
}
